package com.qf.hello;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/20
 */
@ChannelHandler.Sharable
public class ServerSocketHandler extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        System.out.println("服务端读取的内容："+byteBuf.toString(Charset.defaultCharset()));

        //这个channel对象代表当前线程输出的channel对象
        Channel channel = channelHandlerContext.channel();

        //给缓冲区添加数据
        byteBuf.writeBytes("_服务端".getBytes("utf-8"));

        //返回给客户端
        channel.writeAndFlush(byteBuf);
    }
}
